﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Warnning : MonoBehaviour
{
    public Text textCom;

    Action onClick;

    class WarnningIniter : PopupManager.PopupInitter
    {
        private string text;
        Action onClick;

        public WarnningIniter(string text,Action onClick)
        {
            this.text = text;
            this.onClick = onClick;
        }

        public GameObject GetPopupObject()
        {
            UnityEngine.Object prefabObj = Resources.Load("prefabs/warnning");
            if(null == prefabObj)
            {
                Debug.LogError("Resources load prefabs/warnning failed.");
                return null;
            }

            GameObject go = (GameObject)Instantiate(prefabObj);
        
            return go;
        }

        public bool Init(GameObject go)
        {
            var self = go.GetComponent<Warnning>();
            self.textCom.text = text;
            self.onClick = onClick;
            return true;
        }
    }

    public static bool Show(string text,Action onClick = null)
    {
        var action = new WarnningIniter(text,onClick);

        return PopupManager.Instance().Push(action);
    }

    public void OnClick()
    {
        Destroy(gameObject);

        if(onClick != null)
        {
            onClick();
        }
    }

    void OnDestroy()
    {
        PopupManager.Instance().PopNext();
    }
}
